package com.avic.rong.userOrder.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.userOrder.event.InformUpdate;
import com.avic.rong.userOrder.bean.ShippingListBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * Created by XinEr on 2019/7/12.
 * Describe:
 */
public class KuaiDiWayListAdapter extends RecyclerView.Adapter<KuaiDiWayListAdapter.ViewHolder> {
    private Context context;
    public int mSelectedPos=-1;//保存当前选中的position
    private ArrayList<ShippingListBean> shipList;
    private String selectWay;

    public String getSelectWay() {
        return selectWay;
    }

    public void setSelectWay(String selectWay) {
        this.selectWay = selectWay;
    }


    public void setmSelectedPos(int mSelectedPos) {
        this.mSelectedPos = mSelectedPos;
    }

    public KuaiDiWayListAdapter(Context context, ArrayList<ShippingListBean> shipList) {
        this.context = context;
        this.shipList=shipList;
    }

    @NonNull
    @Override
    public KuaiDiWayListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.kuaidi_way_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull KuaiDiWayListAdapter.ViewHolder holder, final int position) {
        if (shipList.get(position).getShippingName().equals("EMS国内邮政特快专递")){
            holder.tv_tag_way.setText("EMS快递");
        }else {
            holder.tv_tag_way.setText(shipList.get(position).getShippingName());
        }

//        if(payloads.isEmpty()) {//payloads即有效负载，当首次加载或调用notifyDatasetChanged() ,notifyItemChange(int position)进行刷新时，payloads为empty 即空
////            holder.tv_tag_way.setText(payloads.get(position).getName());
//            holder.tv_tag_way.setRJCPSelected(mSelectedPos==position);
//            holder.tv_tag_way.setBackground(context.getResources().getDrawable(R.drawable.shape_r20_f6f6f6));
//        }else {//当调用notifyItemChange(int position, Object payload)进行布局刷新时，payloads不会empty ，所以真正的布局刷新应该在这里实现
//            holder.tv_tag_way.setRJCPSelected(mSelectedPos==position);
//            holder.tv_tag_way.setBackground(context.getResources().getDrawable(R.drawable.shape_r20_f6f6f6));
//        }
        if (getSelectWay().equals(shipList.get(position).getShippingName())){
            holder.tv_tag_way.setBackground(context.getResources().getDrawable(R.drawable.shape_r20_s1_f4f9e7));
        }
        if (mSelectedPos==position){
            holder.tv_tag_way.setBackground(context.getResources().getDrawable(R.drawable.shape_r20_s1_f4f9e7));
        }else if (mSelectedPos!=-1){
            holder.tv_tag_way.setBackground(context.getResources().getDrawable(R.drawable.shape_r20_f6f6f6));
        }

        holder.tv_tag_way.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if(mSelectedPos!=position){//当前选中的position和上次选中不是同一个position 执行
//                    holder.tv_tag_way.setRJCPSelected(true);
//                    holder.tv_tag_way.setBackground(context.getResources().getDrawable(R.drawable.shape_r20_s1_f4f9e7));
//                    if(mSelectedPos!=-1){//判断是否有效 -1是初始值 即无效 第二个参数是Object 随便传个int 这里只是起个标志位
//                        notifyItemChanged(mSelectedPos,0);
//                    }
//                    mSelectedPos=position;
//                }

                setmSelectedPos(position);
                notifyDataSetChanged();

                InformUpdate informUpdate = new InformUpdate();
                informUpdate.setKuaidiWay(shipList.get(position).getShippingName());
                informUpdate.setKuaidiWayId(shipList.get(position).getShippingId()+"");
                informUpdate.setEventType(InformUpdate.KUAIDI_WAY_INFORM);
                EventBus.getDefault().post(informUpdate);
            }
        });
    }




    @Override
    public int getItemCount() {
        return shipList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_tag_way;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_tag_way=itemView.findViewById(R.id.tv_tag_way);
        }
    }

}
